package com.katze.common.ws.protocol;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Date;

@JsonInclude(JsonInclude.Include.NON_NULL)  // 忽略null字段，减少传输数据量
public class Message implements Serializable{

    // 通知唯一标识
    private String id;

    // 消息类型（AUTH：用户认证，MESSAGE：消息，CHAT：聊天，SHELL：控制台）
    private MessageType type;

    // 消息编码
    private int code = 200;

    // 数据
    private Object data;

    // 发送者
    private Serializable sender;

    // 发送时间
    private Date timestamp;

    public Message () {}

    public Message (MessageType type, Object data) {
        this(null, type, data);
    }

    public Message (String id, MessageType type, Object data) {
        this.id = id;
        this.type = type;
        this.data = data;
        this.sender = "SYSTEM";
        this.timestamp = new Date();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public MessageType getType() {
        return type;
    }

    public void setType(String type) {
        this.type = MessageType.resolve(type);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public Message setData(Object data) {
        this.data = data;
        return this;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public Serializable getSender() {
        return sender;
    }

    public void setSender(Serializable sender) {
        this.sender = sender;
    }

    public Notification notification(String title, String message) {
        Notification notification = new Notification(title, message);
        setData(notification);
        setTimestamp(new Date());
        return notification;
    }
}
